//
//  LiveNoticeHeader.m
//  YouinRTCDemo
//
//  Created by super on 2021/11/12.
//

#import "LiveNoticeHeader.h"

@implementation LiveNoticeHeader

- (instancetype)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
        [self initUI];
        self.model = [LiveNoticeModel new];
        self.model.type = 1;
    }
    
    return  self;
}

-(void)initUI{
    
    self.backgroundColor = rgba(245, 246, 250, 1);
    
    self.leftButton = [[UIFactory shareInstace] setButton:UIButtonTypeCustom frame:CGRectMake(40, 14.5, 100, 15) image:[UIImage imageNamed:@"unSelect"] title:@"跑马灯公告" font:[UIFont systemFontOfSize:16] backColot:[UIColor clearColor] textColor:rgba(51, 51, 51, 1) target:self action:@selector(buttonAction:) layer:0];
    [self.leftButton setImage:[UIImage imageNamed:@"select"] forState:UIControlStateSelected];
    self.leftButton.selected = true;
    [self.leftButton layoutButtonWithEdgeInsetsStyle:GLButtonEdgeInsetsStyleLeft imageTitleSpace:5];
    [self addSubview:self.leftButton];
    
    self.rightButton = [[UIFactory shareInstace] setButton:UIButtonTypeCustom frame:CGRectMake(SCREEN_WIDTH-100-30, 14.5, 100, 15) image:[UIImage imageNamed:@"unSelect"] title:@"弹窗公告" font:[UIFont systemFontOfSize:16] backColot:[UIColor clearColor] textColor:rgba(51, 51, 51, 1) target:self action:@selector(buttonAction:) layer:0];
    [self.rightButton setImage:[UIImage imageNamed:@"select"] forState:UIControlStateSelected];
    [self addSubview:self.rightButton];
    
    self.inputTextView = [[UITextView alloc] initWithFrame:CGRectZero];
    self.inputTextView.font = [UIFont systemFontOfSize:16];
    self.inputTextView.backgroundColor = [UIColor whiteColor];
    self.inputTextView.layer.cornerRadius = 3;
    self.inputTextView.layer.masksToBounds = YES;
    [self.rightButton layoutButtonWithEdgeInsetsStyle:GLButtonEdgeInsetsStyleLeft imageTitleSpace:5];
    [self addSubview:self.inputTextView];
    
    self.sendButton = [[UIFactory shareInstace] setButton:UIButtonTypeCustom frame:CGRectZero image:nil title:@"发送" font:[UIFont systemFontOfSize:14] backColot:rgba(25, 148, 255, 1) textColor:[UIColor whiteColor] target:self action:@selector(sendAction) layer:3];
    [self addSubview:self.sendButton];
}

-(void)layoutSubviews{
    [super layoutSubviews];
    [self.inputTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self).offset(15);
        make.top.mas_equalTo(self).offset(41);
        make.height.mas_equalTo(28);
        make.right.mas_equalTo(self.sendButton.mas_left).offset(-10);
    }];
    
    [self.sendButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self).offset(41);
        make.height.mas_equalTo(28);
        make.width.mas_equalTo(55);
        make.right.mas_equalTo(self).offset(-15);
    }];
    
}

-(void)buttonAction:(UIButton *)sender{
    if([sender isEqual:self.leftButton]){
        self.leftButton.selected = true;
        self.rightButton.selected = false;
        self.model.type = 1;
    }else{
        self.leftButton.selected = false;
        self.rightButton.selected = true;
        self.model.type = 2;
    }
}

-(void)sendAction{
    
    
    if([self.inputTextView.text isEqualToString:@""]){
        [[ZXMessageCase shareInstace] showMessage:@"请输入公告"];
        return;
    }
    self.model.content = self.inputTextView.text;
    
    [[YouinRTCManager shardManager] sendNotice:self.model success:^(bool success) {
        self.inputTextView.text = @"";
        [[ZXMessageCase shareInstace] showMessage:@"发送成功"];
        if(self.block){
            self.block(self.model.content);
        }
        
    } failure:^(NSError * _Nonnull error) {
        [[ZXMessageCase shareInstace] showMessage:@"发送失败"];
    }];
}

-(void)sendModel{
    [[YouinRTCManager shardManager] sendNotice:self.model success:^(bool success) {
        [[ZXMessageCase shareInstace] showMessage:@"发送成功"];
        if(self.block){
            self.block(self.model.content);
        }
        
    } failure:^(NSError * _Nonnull error) {
        [[ZXMessageCase shareInstace] showMessage:@"发送失败"];
    }];
}

@end
